﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Resources;
using System.Web;
using System.Web.Mvc;
using NC.Infrastructure;
using NC.Infrastructure.Data;
using NC.Infrastructure.Domain.Identity;
using NC.Service.Person;
using domainEntity=NC.Infrastructure.Domain.Identity.Person;

namespace NC.Web.Areas.Admin.Controllers
{
    public class PersonController : Controller
    {
        private readonly IPersonService _personService;
        private readonly NCDbContext _context;

        public PersonController(IPersonService personService,NCDbContext context)
        {
            this._personService = personService;
            this._context = context;
        }

        // GET: Admin/Person
        public ActionResult Index(string search="",int audit=-1,int role=-1,int page=0)
        {
            var data = _personService.GetAll();
            if (!string.IsNullOrEmpty(search))
                data =
                    data.Where(
                        n => n.Name.Contains(search) || n.TickName.Contains(search) || n.SchoolNumber.Contains(search));

            if (audit != -1)
            {
                bool auditCheck = audit == 1;
                data = data.Where(n => n.Audit == auditCheck);
            }

            if (role != -1)
            {
                data = data.Where(n => n.Role == (RoleEnum) role);
            }

            ViewBag.count = data.Count();
            return View(data.PageList(page).ToList());
        }

        public ActionResult Details(int id)
        {
            var person = _personService.Get(id);
            ViewBag.targets = _personService.GetTargets(id).ToList();
            ViewBag.id = id;
            return View(person);
        }

        //用作页面跳转的函数---这里可以作为一种中间保护机制，提高网站的安全性
        public ActionResult ToContentParams(int personId, string targetId)
        {
            Session["personId"] = personId;
            Session["targetId"] = targetId;
            return RedirectToAction("Index", "ContentParams", new {targetId = targetId});
        }


        // GET: Person/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Person/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(domainEntity entity)
        {
            if (ModelState.IsValid)
            {
                _personService.Insert(entity);
                _personService.Commit();
                return RedirectToAction("Index");
            }
            return View(entity);
        }

        // GET: Person/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Person Person = _personService.Get(id);
            if (Person == null)
            {
                return HttpNotFound();
            }
            return View(Person);
        }

        // POST: Person/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(domainEntity entity)
        {
            if (ModelState.IsValid)
            {
                _personService.Update(entity);
                _personService.Commit();
                return RedirectToAction("Index");
            }
            return View(entity);
        }

        // POST: Person/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            string[] ids = id.Split('|');
            int _id;
            foreach (var item in ids)
            {
                _id = Convert.ToInt32(item);
                var _entity = _personService.Get(_id);
                _personService.Delete(_entity);
            }
            _personService.Commit();
            return Json("", JsonRequestBehavior.AllowGet);
        }
    }
}